package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SuppressWarnings("all")
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 去搜索后的页面
     * @param searchParam
     * @param model
     * @return
     */
    @GetMapping("/list.html")
    public String search(SearchParam searchParam, Model model){
        Result<Map> list = listFeignClient.list(searchParam);
        model.addAllAttributes(list.getData());
        // 记录拼接url；
        String urlParam = makeUrlParam(searchParam);
        model.addAttribute("searchParam",searchParam);
        model.addAttribute("urlParam",urlParam);
        //面包屑
        String tradeMarkParam=makeTradeMark(searchParam.getTrademark());
        model.addAttribute("trademarkParam",tradeMarkParam);
        List<Map<String,String>> propsParamList =makeProps(searchParam.getProps());
        model.addAttribute("propsParamList",propsParamList);
        Map<String,String> orderMap=makeOrder(searchParam.getOrder());
        model.addAttribute("orderMap",orderMap);
        return "list/index";
    }

    /**
     * 排序
     * @param order
     * @return
     */
    private Map<String, String> makeOrder(String order) {
        Map<String,String> orderMap=new HashMap<>();
//        1:hotScore 2:price
//        order=1:desc
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if (split!=null&&split.length==2){
                orderMap.put("type",split[0]);
                orderMap.put("sort",split[1]);
            }
        }else {
            orderMap.put("type","1");
            orderMap.put("sort","desc");
        }
        return orderMap;
    }

    /**
     * 平台属性面包屑
     * @param props
     * @return
     */
    private List<Map<String, String>> makeProps(String[] props) {
        List<Map<String,String>> propsParamList=new ArrayList<>();
        if (props!=null&&props.length>0){
//            props=23:4G:运行内存
            for (String prop : props) {
                Map<String,String> map=new HashMap<>();
                String[] split = prop.split(":");
                if (split!=null&&split.length==3){
                    map.put("attrId",split[0]);
                    map.put("attrValue",split[1]);
                    map.put("attrName",split[2]);
                }
                propsParamList.add(map);
            }
        }


        return propsParamList;
    }

    /**
     * 品牌面包屑
     * @param trademark
     * @return
     */
    private String makeTradeMark(String trademark) {
        if (!StringUtils.isEmpty(trademark)){
            String[] split = trademark.split(":");
            if (split!=null && split.length==2){
                return "品牌：" +split[1];
            }
        }
        return "";
    }

    /**
     * 拼接路径
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder urlParam = new StringBuilder();
        // 判断关键字
        if (searchParam.getKeyword()!=null){
            urlParam.append("keyword=").append(searchParam.getKeyword());
        }
        // 判断一级分类
        if (searchParam.getCategory1Id()!=null){
            urlParam.append("category1Id=").append(searchParam.getCategory1Id());
        }
        // 判断二级分类
        if (searchParam.getCategory2Id()!=null){
            urlParam.append("category2Id=").append(searchParam.getCategory2Id());
        }
        // 判断三级分类
        if (searchParam.getCategory3Id()!=null){
            urlParam.append("category3Id=").append(searchParam.getCategory3Id());
        }
        // 处理品牌
        if(searchParam.getTrademark()!=null){
            if (urlParam.length()>0){
                urlParam.append("&trademark=").append(searchParam.getTrademark());
            }
        }
        // 判断平台属性值
        if (null != searchParam.getProps()){
            for (String prop : searchParam.getProps()) {
                if (urlParam.length() > 0){
                    urlParam.append("&props=").append(prop);
                }
            }
        }
        return "list.html?" + urlParam.toString();
    }

}
